package com.bmps.springcloud.insuredmicroservice.callback.accept;

import com.bmps.springcloud.common.response.TaskResponseModel;
import com.bmps.springcloud.insuredmicroservice.constant.accept.AcceptConstant;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.ProductModel;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.RequestModel;
import com.bmps.springcloud.insuredmicroservice.model.underwrite.InsuredModel;
import com.bmps.springcloud.insuredmicroservice.service.underwrite.strategy.risk.InsuranceRiskStrategy;
import com.bmps.springcloud.insuredmicroservice.service.underwrite.strategy.risk.GeneralRiskStrategyFactory;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.concurrent.Callable;

/**
 * description:
 * 风险保额校验任务
 *
 * @author jackdaw
 * @date 2020-06-12 17:44
 */
@Slf4j
public class GeneralRiskInsuranceCallBack implements Callable<TaskResponseModel> {
    private final String key;
    private final ProductModel productModel;
    private final RequestModel requestModel;

    public GeneralRiskInsuranceCallBack(String key, ProductModel productModel, RequestModel requestModel) {
        this.key = key;
        this.productModel = productModel;
        this.requestModel = requestModel;
    }

    @Override
    public TaskResponseModel call() throws Exception {


        TaskResponseModel taskResponseModel = new TaskResponseModel();
        taskResponseModel.setKey(key);
        taskResponseModel.setResultCode(AcceptConstant.RESULT_SUCCESS_CODE);
        taskResponseModel.setResultMessage(AcceptConstant.RESULT_SUCCESS_MESSAGE);
        InsuranceRiskStrategy strategy = GeneralRiskStrategyFactory.getStrategy(productModel.getProdCode());
        if (strategy == null) {
            taskResponseModel.setResultCode(AcceptConstant.RESULT_FAIL_CODE);
            taskResponseModel.setResultMessage(AcceptConstant.NO_EXISTS_STRATEGY);
            return taskResponseModel;
        }
        try {
            InsuredModel insuredModel = strategy.checkProductRiskQuota(productModel, requestModel);
            if (!StringUtils.equals(insuredModel.getResultCode(), AcceptConstant.RESULT_SUCCESS_CODE)) {
                taskResponseModel.setResultCode(insuredModel.getResultCode());
                taskResponseModel.setResultMessage(insuredModel.getResultMessage());
            }
            log.info("【风险保额校验結果】" + taskResponseModel.toString());
            return taskResponseModel;
        } catch (Exception e) {
            log.error("【" + requestModel.getPolicyInfo().getContNo() + "】", e);
            taskResponseModel.setResultCode(AcceptConstant.RESULT_FAIL_CODE);
            taskResponseModel.setResultMessage("风险保额校验:" + AcceptConstant.RESULT_FAIL_MESSAGE);
            return taskResponseModel;
        }
    }
}
